package com.librariy.view;
import java.util.Map;
import com.librariy.net.SSLWebViewClient;
import com.librariy.util.Log;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
/**
 * WebView扩展控件，带下拉刷新网页功能
 * 
 * @author luchyu
 */
public class SwipeRefreshWebView extends SwipeRefreshLayout implements SwipeRefreshLayout.OnRefreshListener {
    public static final String TAG = "SwipeRefreshWebView";
    private WebView mWebView;
    public SwipeRefreshWebView(Context mContext) {
        super(mContext);
        this.initialise(mContext);
    }
    public SwipeRefreshWebView(Context mContext, AttributeSet attrs) {
        super(mContext, attrs);
        this.initialise(mContext);
    }
    @SuppressLint("SetJavaScriptEnabled")
    public void initialise(Context mContext) {
        super.setColorSchemeColors(0xFFFF0000, 0xFF00FF00,0xFF0000FF,0xFFFF00FF);
        super.setProgressViewOffset(false, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics()));
        this.mWebView=new WebView(getContext());
        this.mWebView.setVerticalScrollBarEnabled(false);
        this.mWebView.setHorizontalScrollBarEnabled(false);
        this.mWebView.getSettings().setJavaScriptEnabled(true);
        this.mWebView.getSettings().setDomStorageEnabled(true);
        //自适应屏幕
        this.mWebView.getSettings().setUseWideViewPort(true);
        this.mWebView.getSettings().setLoadWithOverviewMode(true);
        this.mWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
        //
        this.mWebView.setWebViewClient(new SSLWebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                loadUrl(url,null);
                return true;
            }
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                Log.i(TAG, "onPageStarted(), URL="+url+"");
                setRefreshing(true);
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                setRefreshing(false);
            }
        });
        super.addView(mWebView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        super.setOnRefreshListener(this);
    }

    public void loadUrl(String mUrl,Map<String,String> additionalHttpHeaders) {
        Log.i(TAG, "loadUrl, URL="+mUrl+"; additionalHttpHeaders="+additionalHttpHeaders);
        this.mWebView.loadUrl(mUrl, additionalHttpHeaders);
    }
    public void loadData(String data, String mimeType, String encoding) {
        this.mWebView.loadData(data, mimeType, encoding);
    }
    @SuppressLint("JavascriptInterface")
    public void addJavascriptInterface(Object obj, String interfaceName) {
        this.mWebView.addJavascriptInterface(obj, interfaceName);
    }
    public void setWebChromeClient(WebChromeClient webChromeClient) {
        this.mWebView.setWebChromeClient(webChromeClient);
    }
    public void evaluateJavascript(String jsScript, ValueCallback<String> resultCallback) {
        if(Build.VERSION.SDK_INT>19){
            this.mWebView.evaluateJavascript(jsScript,resultCallback);
        }else{
            this.mWebView.loadUrl(jsScript,null);
        }
    }
    /**
     * 调用返回上一个页面。
     * 
     * @return 如果成功返回到上一个页面，则返回true,否则返回false
     */
    public boolean onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return false;
    }

    @Override
    public void onRefresh() {
        mWebView.reload();
    }
}
